package handlers

import (
    "net/http"

    "github.com/gin-gonic/gin"

    "github.com/samaa/photosalesplus/backend/internal/services"
)

type MerchantAuthHandler struct{ svc services.MerchantAuthService }

func NewMerchantAuthHandler(s services.MerchantAuthService) *MerchantAuthHandler { return &MerchantAuthHandler{svc: s} }

type merchantLoginRequest struct {
    Username string `json:"username" binding:"required"`
    Password string `json:"password" binding:"required"`
}

func (h *MerchantAuthHandler) Login(c *gin.Context) {
    var req merchantLoginRequest
    if err := c.ShouldBindJSON(&req); err != nil {
        c.JSON(http.StatusBadRequest, APIResponse{Error: err.Error()})
        return
    }
    token, m, err := h.svc.Login(c.Request.Context(), req.Username, req.Password)
    if err != nil {
        c.JSON(http.StatusUnauthorized, APIResponse{Error: err.Error()})
        return
    }
    c.JSON(http.StatusOK, APIResponse{Data: gin.H{
        "token": token,
        "merchant": gin.H{
            "id":           m.ID,
            "username":     m.Username,
            "display_name": m.DisplayName,
            "email":        m.Email,
        },
    }})
}

